package cn.com.shaom.learning.sb.security.example2.service;

import cn.com.shaom.learning.sb.security.example2.User;
import cn.com.shaom.learning.sb.security.example2.dao.SysUserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * ClassName: CumDetailsService
 * Description: 自定义 UserDetailsService
 * Date: 2017/12/27 15:36 【需求编号】
 *
 * @author Shaom
 * @version V1.0.0
 */
public class CumDetailsService implements UserDetailsService {

    @Resource
    private SysUserRepository sysUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = sysUserRepository.findByName(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }
}
